import request from '@/utils/requets'
import type {
    ReturnUsers,
    User,
    ReturnRoles,
    RoleInfo,
    DeleteUser,
} from './type'

enum API {
    // 获取用户列表
    USERDATA_URL = '/admin/acl/user/',
    // 新增用户
    ADDUSER_URL = '/admin/acl/user/save',
    // 更新用户
    UPDATEUSER_URL = '/admin/acl/user/update',
    // 删除用户
    DELETEUSER_URL = '/admin/acl/user/remove/',
    //获取全部职位,当前账号拥有的职位接口
    ALLROLEURL = '/admin/acl/user/toAssign/',
    //给已有的用户分配角色接口
    SETROLE_URL = '/admin/acl/user/doAssignRole',
    //批量删除的接口
    DELETEALLUSER_URL = '/admin/acl/user/batchRemove',
}
// 获取用户列表
export const getUser = (pageNo: number, pageSize: number, username: string) =>
    request.get<any, ReturnUsers>(
        API.USERDATA_URL + `${pageNo}/${pageSize}/?username=${username}`,
    )

// 新增或更新
export const addOrUpdateUser = (data: User) => {
    if (data.id) {
        return request.post<any, any>(API.UPDATEUSER_URL, data)
    } else {
        return request.put<any, any>(API.ADDUSER_URL, data)
    }
}
// 获取用户职位
export const toAssign = (adminId: number) =>
    request.get<any, ReturnRoles>(API.ALLROLEURL + adminId)

export const doAssignRole = (data: RoleInfo) =>
    request.post<any, any>(API.SETROLE_URL, data)

// 删除用户
export const removeUser = (id: number) =>
    request.delete<any, any>(API.DELETEUSER_URL + id)

export const batchRemove = (data: DeleteUser) =>
    request.delete<any, any>(API.DELETEALLUSER_URL, { data })
